package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.EditNewsListAdapter;
import com.lc.luzhengtang.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/15.
 */
public class EditNewsListActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.text_biaoji)
    TextView textBiaoji;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private static final int TOTAL_COUNTER = 18;

    private static final int PAGE_SIZE = 6;

    EditNewsListAdapter indexAdapter;

    List<String> mDatas;
    List<String> mDatas2;
    private int mCurrentCounter = 0;
    private boolean isErr;
    private boolean mLoadMoreEndGone = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_newslist);
        ButterKnife.bind(this);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        rvList.setLayoutManager(new LinearLayoutManager(this));
        mDatas = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mDatas.add("干他妈的" + i);
        }
        mDatas2 = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mDatas2.add("特么的傻吊" + i);
        }
        initAdapter();
    }

    private void initAdapter() {
        indexAdapter = new EditNewsListAdapter(mDatas);
        indexAdapter.setOnLoadMoreListener(this);
        // pullToRefreshAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        indexAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
//        pullToRefreshAdapter.setAutoLoadMoreSize(3);
        rvList.setAdapter(indexAdapter);
        mCurrentCounter = indexAdapter.getData().size();

        rvList.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Toast.makeText(EditNewsListActivity.this, Integer.toString(position), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void onRefresh() {
        indexAdapter.setEnableLoadMore(false);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                indexAdapter.setNewData(mDatas2);
                isErr = false;
                mCurrentCounter = PAGE_SIZE;
                swipeLayout.setRefreshing(false);
                indexAdapter.setEnableLoadMore(true);


            }
        }, 1000);
    }

    @Override
    public void onLoadMoreRequested() {
        swipeLayout.setEnabled(false);
        rvList.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (indexAdapter.getData().size() < PAGE_SIZE) {
                    indexAdapter.loadMoreEnd(true);
                } else {
                    if (mCurrentCounter >= TOTAL_COUNTER) {
//                    pullToRefreshAdapter.loadMoreEnd();//default visible
                        indexAdapter.loadMoreEnd(mLoadMoreEndGone);//true is gone,false is visible
                    } else {
                        if (isErr) {
                            //  pullToRefreshAdapter.addData(DataServer.getSampleData(PAGE_SIZE));
                            //   indexAdapter.add(mDatas2.add("1111"));
                            mCurrentCounter = indexAdapter.getData().size();
                            indexAdapter.loadMoreComplete();
                            Toast.makeText(context, "加载成功", Toast.LENGTH_SHORT).show();

                        } else {
                            isErr = true;
                            Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                            indexAdapter.loadMoreFail();

                        }
                    }
                    swipeLayout.setEnabled(true);
                }
            }

        }, 1000);
    }

    @OnClick({R.id.iv_back, R.id.text_biaoji})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:

                finish();
                break;
            case R.id.text_biaoji:

                // startActivity(new Intent(NewsListActivity.this,EditNewsListActivity.class));

                break;
        }
    }

}
